<!--   [PAGE]] : 个人工作记录
       @Author  : ljq
       @Created : 2025/10/12
 -->
<template>
  <div class="app-container">
    <!-- ------------------------ START: 查询框 -------------------------->
    <el-form class="query" :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="项目">
        <el-input v-model="queryParams.params.projectNameLike" placeholder="请输入项目" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="银行卡">
        <select-bank-card v-model="queryParams.params.bankCardIdList" placeholder="请选择银行卡"/>
      </el-form-item>
      <el-form-item label="日期">
        <el-input v-model="queryParams.params.workDateLike" placeholder="请输入日期" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryParams.status" placeholder="状态" filterable clearable>
          <el-option v-for="dict in dict.type.business_work_order_status" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>

<!--      <el-form-item label="审批人">-->
<!--        <select-user v-model="queryParams.params.approvalByList" placeholder="请选择审批人"/>-->
<!--      </el-form-item>-->

      <el-form-item class="option">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- ------------------------- END: 查询框 --------------------------->

    <!-- ---------------------- START: 主数据区域------------------------->
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"/>
    </el-row>


    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column label="编号" align="center" prop="id" v-if="columns[0].visible"/>
      <el-table-column label="项目" align="center" prop="project.name" v-if="columns[1].visible"/>
      <el-table-column label="工人" align="center" prop="userId" v-if="columns[2].visible">
        <template slot-scope="scope">
          <select-user isCol :value="scope.row.userId"/>
        </template>
      </el-table-column>
      <el-table-column label="银行卡" align="center" prop="bankCard.cardId" v-if="columns[3].visible"/>
      <el-table-column label="工作日期" align="center" prop="workDate" v-if="columns[10].visible"/>
      <el-table-column label="工作时间" align="center" prop="planWorkTimeRange" v-if="columns[4].visible"/>
      <el-table-column label="计划工时" align="center" prop="planHour" v-if="columns[7].visible"/>
      <el-table-column label="上班时间" align="center" prop="realWorkStartAt" v-if="columns[5].visible"/>
      <el-table-column label="下班时间" align="center" prop="realWorkEndAt" v-if="columns[6].visible"/>
      <el-table-column label="实际工时" align="center" prop="realHour" v-if="columns[8].visible"/>
      <el-table-column label="备注" align="center" prop="comment" v-if="columns[9].visible"/>
      <el-table-column label="状态" align="center" prop="status" v-if="columns[11].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.business_work_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="审批人" align="center" prop="approvalBy" v-if="columns[12].visible">
        <template slot-scope="scope">
          <select-user :isCol="true" :value="scope.row.approvalBy"/>
        </template>
      </el-table-column>
      <el-table-column label="驳回原因" align="center" prop="approvalComment" v-if="columns[13].visible"/>
      <el-table-column label="时薪" align="center" prop="hourlyWage" v-if="columns[14].visible"/>
      <el-table-column label="工种" align="center" prop="workType" v-if="columns[15].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.business_work_type" :value="scope.row.workType"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" v-if="columns[16].visible"/>
      <el-table-column label="创建者" align="center" prop="createBy" v-if="columns[17].visible"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" v-if="columns[18].visible"/>
      <el-table-column label="更新者" align="center" prop="updateBy" v-if="columns[19].visible"/>


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="['1'].includes(scope.row.status)"
                     size="mini" type="text" icon="el-icon-edit"
                     @click="handleUpdate(scope.row)" v-hasPermi="[`${permi}:edit`]"
          >工时提交
          </el-button>
          <el-button v-if="['3'].includes(scope.row.status)"
                     size="mini" type="text" icon="el-icon-edit"
                     @click="handleUpdate2(scope.row)" v-hasPermi="[`${permi}:edit`]"
          >重新提交
          </el-button>
          <el-button v-if="['2'].includes(scope.row.status)"
                     size="mini" type="text" icon="el-icon-edit"
                     @click="handleBack(scope.row)" v-hasPermi="[`${permi}:edit`]"
          >撤销
          </el-button>
        </template>
      </el-table-column>

    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>
    <!-- ---------------------- END: 主数据区域 ------------------------>

    <!-- ------------------------ START: 弹出框 -------------------------->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="银行卡" prop="bankCardId">
              <select-bank-card v-model="form.bankCardId" placeholder="请选择银行卡"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上班时间" prop="realWorkStartAt">
              <el-time-select
                v-model="form.realWorkStartAt"
                :picker-options="{
                  start: '00:30',
                  step: '00:15',
                  end: '23:30'
                }"
                placeholder="选择时间">
              </el-time-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上班时间" prop="realWorkStartAt">
              <el-time-select
                v-model="form.realWorkEndAt"
                :picker-options="{
                  start: '00:30',
                  step: '00:15',
                  end: '23:30'
                }"
                placeholder="选择时间">
              </el-time-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实际工时" prop="realHour">
              <el-input v-model="form.realHour" placeholder="请输入实际工时"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="comment">
              <el-input type="textarea" v-model="form.comment" placeholder="请输入备注"/>
            </el-form-item>
          </el-col>
        </el-row>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- ------------------------ END: 弹出框 -------------------------->
  </div>
</template>
<script>
import {listProjectDetail, getProjectDetail, addProjectDetail, updateProjectDetail, delProjectDetail} from "@/api/business/projectDetail";
import {initData, baseMethods} from "@/utils/basePage";


let baseData = initData();
baseData.baseTitle = "";
baseData.form.status = '1';


export default {
  name: "MyWork",
  dicts: ['business_work_type', 'business_work_order_status'],
  components: {},
  data() {
    return {
      ...baseData,
      permi: 'business:projectDetail',
      // 列信息
      columns: [
        {key: 0, label: `编号`, visible: false},
        {key: 1, label: `项目`, visible: true},
        {key: 2, label: `工人`, visible: false},
        {key: 3, label: `银行卡`, visible: true},
        {key: 4, label: `上班时间区间`, visible: true},
        {key: 5, label: `上班时间`, visible: true},
        {key: 6, label: `下班时间`, visible: true},
        {key: 7, label: `计划工时`, visible: true},
        {key: 8, label: `实际工时`, visible: true},
        {key: 9, label: `备注`, visible: true},
        {key: 10, label: `日期`, visible: true},
        {key: 11, label: `状态`, visible: true},
        {key: 12, label: `审批人`, visible: true},
        {key: 13, label: `驳回原因`, visible: true},
        {key: 14, label: `时薪`, visible: true},
        {key: 15, label: `工种`, visible: false},
        {key: 16, label: `创建时间`, visible: false},
        {key: 17, label: `创建者`, visible: false},
        {key: 18, label: `更新时间`, visible: false},
        {key: 19, label: `创建者`, visible: false},
      ],
      // 表单校验
      rules: {
        projectId: [{required: true, message: "项目不能为空", trigger: "blur"}],
        userId: [{required: true, message: "工人不能为空", trigger: "blur"}],
        bankCardId: [{required: true, message: "银行卡不能为空", trigger: "blur"}],
        planWorkTimeRange: [{required: true, message: "上班时间区间不能为空", trigger: "blur"}],
        realWorkStartAt: [{required: true, message: "上班时间不能为空", trigger: "blur"}],
        realWorkEndAt: [{required: true, message: "下班时间不能为空", trigger: "blur"}],
        planHour: [{required: true, message: "计划工时不能为空", trigger: "blur"}],
        realHour: [{required: true, message: "实际工时不能为空", trigger: "blur"}],
        hourlyWage: [{required: true, message: "时薪不能为空", trigger: "blur"}],
        delFlag: [{required: true, message: "删除标志不能为空", trigger: "blur"}],
      }
    };
  },
  created() {
    const currentUser = this.$store.state.user;
    // this.buttonControl.common = currentUser.roles.includes('master');
    // this.buttonControl.common = true;

    // this.queryParams.userId = currentUser.id;


    this.getList();
  },
  methods: {
    ...baseMethods(listProjectDetail, getProjectDetail, addProjectDetail, updateProjectDetail, delProjectDetail),

    // 撤销
    handleBack(row) {
      row.status = '1';
      // WARNING: 数字类型置空无效
      row.realHour = 0;
      row.realWorkStartAt = '';
      row.realWorkEndAt = '';
      row.approvalComment = '';
      row.comment = '';
      updateProjectDetail(row).then(response => {
        this.$modal.msgSuccess("撤销成功");
        this.getList();
      });
    },

    // 重新提交
    handleUpdate2(row) {
      // this.handleUpdate(row);
      this.form = {...row}
      this.form.status = '1';
      this.form.realHour = '';
      this.form.realWorkStartAt = '';
      this.form.realWorkEndAt = '';
      this.form.approvalComment = '';
      this.form.comment = '';
      this.open = true;
    },

  }
};
</script>
